package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisMessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.SMSUtils;
import com.itheima.common.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/validatecode")
@Slf4j
public class ValidatecodeController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送预约短信
     */
    @PostMapping("/send4Order")
    public Result send4Order(String telephone){
        String code = Integer.toString(ValidateCodeUtils.generateValidateCode(4));
        log.info("预约短信验证码：" + code);
        String key = RedisMessageConstant.SENDTYPE_ORDER + "-" + telephone;
        //SMSUtils.sendMessage(SMSUtils.VALIDATE_CODE, telephone, code);
        redisTemplate.opsForValue().set(key, code, 5, TimeUnit.MINUTES);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    /**
     * 发送登录短信
     */
    @GetMapping("/send4Login")
    public Result send4Login(String telephone){
        String code = Integer.toString(ValidateCodeUtils.generateValidateCode(4));
        log.info("登录短信验证码：" + code);
        String key = RedisMessageConstant.SENDTYPE_LOGIN + "-" + telephone;
        //SMSUtils.sendMessage(SMSUtils.VALIDATE_CODE, telephone, code);
        redisTemplate.opsForValue().set(key, code, 5, TimeUnit.MINUTES);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
